﻿package com.content.model.Communication
{
	import com.content.model.LoadedAssets.PatternFeedProxy;
	import com.library.core.IDisposable;
	
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;

	/**
	 * WebAPIProxy 向 Controller 与 Mediator 提供 WebAPI 访问功能
	 * 其内部通过 WebAPILoader 类实现与 Web后台的互通
	 * */
	public class WebAPIProxy extends Proxy implements IProxy, IDisposable
	{
		public static const NAME:String = "WebAPIProxy";
		
		public static const CheckConnection:String					= "checkConnection";
		public static const GetPurchasedItemsByRes:String		= "getPurchasedItemsByRes";
		public static const GetPublicResourceCount:String		= "getPublicResourceCount";
		public static const GetPublicResourcePerPage:String		= "getPublicResourcePerPage";
		public static const GetPersonalInfo:String					= "getPersonalInfo";
		public static const PurchaseResource:String				= "purchaseResource";
		public static const GetHomeShowCase:String				= "getHomeShowCase";
		public static const GetHomeRecommend:String			= "getHomeRecommend";
		public static const GetHomeNotice:String					= "getHomeNotice";
		public static const GetNoticeList:String						= "getNoticeList";
		public static const GetNoticeDetail:String					= "getNoticeDetail";
		public static const GetNoticeCount:String					= "getNoticeCount";
		public static const GetCategoryPublicBook:String			= "getCategoryPublicBook";
		public static const GetCategoryPublicVideo:String		= "getCategoryPublicVideo";
		public static const GetCategoryPublicAudio:String		= "getCategoryPublicAudio";
		
		private var _url:String;
		private var _userID:String;
		
		public function WebAPIProxy(url:String, userID:String) 
		{
			super(NAME, null);
			
			_url =url;
			_userID = userID;
		}
		
		private function APISend(xml:XML, func:Function):void{
			var request:URLRequest = new URLRequest(_url);
			request.contentType = "text/xml";
			request.method = URLRequestMethod.POST;
			request.data = xml;
			
			var apiLoader:WebAPILoader;
			apiLoader = new WebAPILoader(func);
			apiLoader.Load(request);
		}
		
		/* --------------------------------------------------------------------------------------
		* Communication Command
		* ----------------------------------------------------------------------------------- */
		public function CheckConnection(handler:Function):void{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.CheckConnection);
			dataToSave.head.userid = _userID;
			APISend(dataToSave, handler);
		}
		
		public function GetPurchasedItemsByRes(handler:Function, resource:String=""):void
		{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.GetPurchasedItemsByRes);
			dataToSave.head.userid = _userID;
			var cdResource:XML = new XML("\<![CDATA[" + resource + "]]\>");
			dataToSave.body.filter.resource.appendChild(cdResource);
			APISend(dataToSave, handler);
		}
		
		public function GetPublicResourceCount(handler:Function,
											   resource:String, 
											   category:String, 
											   searchKey:String):void
		{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.GetPublicResourceCount);
			dataToSave.head.userid = _userID;
			
			var cdResource:XML = new XML("\<![CDATA[" + resource + "]]\>");
			var cdCategory:XML = new XML("\<![CDATA[" + category + "]]\>");
			var cdSearchKey:XML = new XML("\<![CDATA[" + searchKey + "]]\>");
			
			dataToSave.body.filter.resource.appendChild(cdResource);
			dataToSave.body.filter.category.appendChild(cdCategory);
			dataToSave.body.filter.searchkey.appendChild(cdSearchKey);
			APISend(dataToSave, handler);
		}
		
		public function GetPublicResourcePerPage(handler:Function,
												 pagenum:int,
												 quantity:int, 
												 resource:String, 
												 category:String, 
												 searchKey:String):void
		{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.GetPublicResourcePerPage);
			dataToSave.head.userid = _userID;
			
			dataToSave.body.pagination.@pagenum = pagenum;
			dataToSave.body.pagination.@quantity = quantity;
			
			var cdResource:XML = new XML("\<![CDATA[" + resource + "]]\>");
			var cdCategory:XML = new XML("\<![CDATA[" + category + "]]\>");
			var cdSearchKey:XML = new XML("\<![CDATA[" + searchKey + "]]\>");
			
			dataToSave.body.filter.resource.appendChild(cdResource);
			dataToSave.body.filter.category.appendChild(cdCategory);
			dataToSave.body.filter.searchkey.appendChild(cdSearchKey);
			APISend(dataToSave, handler);
		}
		
		public function GetPersonalInfo(handler:Function):void
		{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.GetPersonalInfo);
			dataToSave.head.userid = _userID;
			APISend(dataToSave, handler);
		}
		
		public function PurchaseResource(handler:Function,
										 resID:String,
										 resource:String):void
		{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.PurchaseResource);
			dataToSave.head.userid = _userID;
			dataToSave.body.purchase.item.@id = resID;
			dataToSave.body.purchase.item.@resource = resource;
			APISend(dataToSave, handler);
		}
		
		public function GetHomeShowCase(handler:Function):void{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.GetHomeShowCase);
			dataToSave.head.userid = _userID;
			APISend(dataToSave, handler);
		}
		
		public function GetHomeRecommend(handler:Function):void{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.GetHomeRecommend);
			dataToSave.head.userid = _userID;
			APISend(dataToSave, handler);
		}
		
		public function GetHomeNotice(handler:Function):void{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.GetHomeNotice);
			dataToSave.head.userid = _userID;
			APISend(dataToSave, handler);
		}
		
		public function GetNoticeList(handler:Function,
									  pagenum:int,
									  quantity:int):void{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.GetNoticeList);
			dataToSave.head.userid = _userID;
			dataToSave.body.pagination.@pagenum = pagenum;
			dataToSave.body.pagination.@quantity = quantity;
			APISend(dataToSave, handler);
		}
		
		public function GetNoticeDetail(handler:Function, 	noticdID:String):void{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.GetNoticeDetail);
			dataToSave.head.userid = _userID;
			dataToSave.body.notice.@id = noticdID;
			APISend(dataToSave, handler);
		}
		
		public function GetNoticeCount(handler:Function):void{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.GetNoticeCount);
			dataToSave.head.userid = _userID;
			APISend(dataToSave, handler);
		}
		
		public function GetCategoryPublicBook(handler:Function):void
		{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.GetCategoryPublicBook);
			dataToSave.head.userid = _userID;		
			APISend(dataToSave, handler);
		}
		
		public function GetCategoryPublicVideo(handler:Function):void
		{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.GetCategoryPublicVideo);
			dataToSave.head.userid = _userID;
			APISend(dataToSave, handler);
		}
		
		public function GetCategoryPublicAudio(handler:Function):void
		{
			var dataToSave:XML = pPatternFeed.GetFeed(WebAPIProxy.GetCategoryPublicAudio);
			dataToSave.head.userid = _userID;		
			APISend(dataToSave, handler);
		}
		
		public function Dispose():void{
		}
		
		private function get pPatternFeed():PatternFeedProxy{	return facade.retrieveProxy( PatternFeedProxy.NAME ) as PatternFeedProxy;	}
		
	}
}